package cn.mxz.robots.province;

import net.sf.json.JSONObject;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import cn.mxz.crawler_single.basic.Request;
import cn.mxz.robots.basic.local.Exam;
import cn.mxz.robots.pojo.Student;

/**
 * 陕西
 * 排名:完成
 * 
 * @author cuizhennan<cuizhennan@mouge.wang>
 * @since 2015-06-19
 */
public class Shaanxi extends Exam
{

	public static void main(String[] args)
	{
		Shaanxi sx = new Shaanxi(new Student("0703150514", "", "610623199505221020", "", ""));

		System.out.println(sx.login(""));
	}

	public Shaanxi(Student student)
	{
		super(student);
	}

	@Override
	public String login(String captcha)
	{
		Request initRequest = new Request("http://www.sneac.com/gkcxx/cjcx/zcjcx.htm", mSha1);
		curlGet(initRequest);

		Request loginRequest = new Request("http://www.sneac.com/gkcjcx/2011zcjcx_jg.jsp?wbtreeid=3063", mSha1);
//		loginRequest.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko");
//		loginRequest.addHeader("DNT", "1");
		loginRequest.addParams("wbsfzh", mStudent.getIdCard());
		loginRequest.addParams("wbzkzh", mStudent.getStudentId());

		String loginHtml = curlPost(loginRequest);

		System.out.println(loginHtml);

		JSONObject resultObject = new JSONObject();
		if (loginHtml.contains("姓名"))
		{
			resultObject.put("result", LOGIN_SUCCESS);
			resultObject.put("data", loginParseHtml(loginHtml));
		}
		else
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", WRONG_USER_PASSWORD);
		}

		return resultObject.toString();
	}

	@Override
	public String loginParseHtml(String parseHtml)
	{
		Document loginDoc = Jsoup.parse(parseHtml);
		Elements trs = loginDoc.select("#searchlistform1 table tr");

		JSONObject data = new JSONObject();

		data.put("exam_id", trs.get(0).select("td").get(1).text());
		data.put("name", trs.get(0).select("td").get(3).text());
		data.put("ranking", trs.get(2).select("td").get(5).text());
		data.put("province", "陕西省");

		JSONObject score = new JSONObject();
		score.put("语文", trs.get(2).select("td").get(0).text());
		score.put("数学", trs.get(2).select("td").get(1).text());
		score.put("综合", trs.get(2).select("td").get(2).text());
		score.put("外语", trs.get(2).select("td").get(3).text());
		score.put("总分", trs.get(2).select("td").get(4).text());

		data.put("score", score.toString());

		return data.toString();
	}
}
